package com.dhcc.android.base.gis;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;

import java.util.ArrayList;
import java.util.List;

import srs.DataSource.Vector.SearchType;
import srs.Display.IScreenDisplay;
import srs.Element.FillElement;
import srs.Element.IElement;
import srs.Element.IFillElement;
import srs.GPS.GPSControl;
import srs.Geometry.IGeometry;
import srs.Geometry.IPoint;
import srs.Geometry.srsGeometryType;
import srs.Layer.CommonLayer;
import srs.tools.BaseControl;
import srs.tools.BaseTool;

/**
 * 目标对象的点选，针对CommenLayer
 * @author lzy
 * @version 20150707
 *
 */
public class TouchLongToolMultiple extends BaseTool {

	/** 选中目标后的弹窗。 可以根据需要，使用您自己的弹窗； 如屏幕不弹出弹窗，亦可舍弃此项；
	 */
	public PopupWindow mTouchLongPop;
	private PointF mCurrentPoint = null;
	/*private IPoint mPFT = null;*/
	private IScreenDisplay mScreenDisplay;
	private static CommonLayer mLayer;
	/* private List<Integer> mAllfids; */
	private Paint mPaint;
	public static int currentIndex;
	/**选中的要素高亮显示时使用的
	 * 
	 */
	
	private List<IElement> fillElements = null;
	private List<Integer> indexs = new ArrayList<Integer>();
	private List<IGeometry> geos = new ArrayList<IGeometry>();
	/**
	 * 点选时间控制，手指在屏幕上停留最少多久，视为点选操作 可根据业务需要，手动设置此项属性
	 * 此属性也可通过系统配置设置，系统设置修改时，调用此属性进行修改
	 */
	/*private static long LONGTIME = 10;*/
	/**
	 * 点选距离控制，触摸点与目标点最远相聚多少时，可视为选中 可根据业务需要，手动设置此项属性
	 * 此属性也可通过系统配置设置，系统设置修改时，调用此属性进行修改
	 */
	public static float Dis = 10;
	/* private Bitmap mbitmap; */
	private static String mTitleStr = "详细信息";
	/**
	 * MapControl输出的底图
	 * 
	 */
	Bitmap mBitExMap = null;

	/**
	 * 在此添加选中目标后会触发的动作；
	 * 
	 */
	public MultipleItemChangedListener zoom2Selected = null;

	/**
	 * 判断定位辅助变量：记录“ACTION_DOWN”时手指的屏幕位置
	 * 
	 */
	/*private int xposition = 0;
	private int yposition = 0;*/

	/**
	 * 手指触摸屏幕的时刻
	 * 
	 */
	private long mExtTime = -1;

	/**
	 * 是否为选择单条记录
	 * 
	 */
	public boolean IsOnlyOneSelect = true;

	/**
	 * 是否为一次性选择
	 * 
	 */
	public boolean IsOnlyOneTime = true;

	public TouchLongToolMultiple(CommonLayer TargetLayer) {
		zoom2Selected = null;
		mLayer = TargetLayer;
		indexs.clear();
		geos.clear();
		mPaint = new Paint();
		mPaint.setColor(Color.YELLOW);
		mPaint.setStrokeWidth(2);
		mPaint.setStyle(Style.FILL);

		super.setRate();
		if (mLayer != null) {
			if (mLayer.getGeometryType() == srsGeometryType.Point) {
				Dis = 30; // 若目标类型为“点”，则缓冲距离为30
			} else {
				Dis = 10; // 若目标类型为“线、面”缓冲距离为10
			}
		}
	}

	@Override
	public String getText() {
		return mTitleStr;
	}

	@Override
	public Bitmap getBitmap() {
		return null;
	}

	public BaseControl getBuddyControl() {
		return this.mBuddyControl;
	}

	public void setBuddyControl(BaseControl value) {
		if (value != null) {
			this.mBuddyControl = value;
			/*mBuddyControl.getActiveView().FocusMap().getSelectionSet()
			.ClearSelection();*/
		}
	}

	@Override
	public void onClick(View v) {
		try {
			super.onClick(v);
			if (this.mBuddyControl instanceof BaseControl) {
				mScreenDisplay = this.mBuddyControl.getActiveView().FocusMap()
						.getScreenDisplay();
			}
			mEnable = true;
			mBuddyControl.setDrawTool(this);
			mBuddyControl.PartialRefresh();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see srs.tools.BaseTool#onTouch(android.view.View, android.view.MotionEvent)
	 */
	/* (non-Javadoc)
	 * @see srs.tools.BaseTool#onTouch(android.view.View, android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mBitExMap = mBuddyControl.getActiveView().FocusMap()
			.ExportMap(false);
			if (mExtTime == -1) {
				mExtTime = System.currentTimeMillis();
				mCurrentPoint = new PointF(event.getX() * mRate, event.getY()
						* mRate);
			}
			/*xposition = (int) event.getX();
			yposition = (int) event.getY();*/
			break;
		case MotionEvent.ACTION_UP:
			try {
				GPSControl mGPSControl = GPSControl.getInstance();
				double latitude = mGPSControl.GPSLatitude;
				double longitude = mGPSControl.GPSLongitude;
				String latlon = String.valueOf(latitude) + ","
						+ String.valueOf(longitude);
				System.out.println("--------------------" + latlon);
				if (event.getPointerCount() == 1
						/* mExtTime!=-1&&System.currentTimeMillis()-mExtTime>LONGTIME
						 * &&Math.abs(event.getX()-xposition)<5
						 * &&Math.abs(event.getY()-yposition)<5
						 */) {
					// 长按
					super.onTouch(v, event);
					mExtTime = -1;
					PointF pF = new PointF(event.getX() * mRate, event.getY()
							* mRate);
					IPoint selGeo = mBuddyControl.ToWorldPoint(new PointF(pF.x,
							pF.y));

					
					if (mCurrentPoint != null
							&& Math.abs(mCurrentPoint.x - pF.x) < Dis
							&& Math.abs(mCurrentPoint.y - pF.y) < Dis) {
						// 在一个点位，此时才算“长点击”
						List<Integer> sels = mLayer.SelectOnlyOne(selGeo,
										SearchType.Intersect,
										mScreenDisplay.ToMapDistance(Dis));
						IFillElement SELECTEDElement = null;
						Integer index = -1;
						if (sels != null && sels.size() > 0) {
							mExtTime = -1;
							index = sels.get(0);
							// 查询图层中选中点所对应的geo
							IGeometry geo = mLayer.getGeometry(index);
							if (indexs.size()>0 && indexs !=null) {
								
								if (indexs.contains(sels.get(0))) {
									indexs.remove(sels.get(0));
									geos.remove(geo);
								}else {
									indexs.add(sels.get(0));
									geos.add(geo);
								}
							}else {
								indexs.add(sels.get(0));
								geos.add(geo);
							}
							currentIndex = index;

							mBuddyControl.getElementContainer().ClearElement();
							for (int i = 0; i < indexs.size(); i++) {
								IFillElement fillElement = new FillElement();
								fillElement.setGeometry(geos.get(i));
								fillElement.setSymbol(srs.Display.Setting.SelectPolygonFeatureStyle);
								mBuddyControl.getElementContainer().AddElement(fillElement);
							}
//							if (SELECTEDElement == null) {
//								SELECTEDElement = new FillElement();
//							}
//							if (fillElements == null) {
//								fillElements = new ArrayList<IElement>();
//							}
//							SELECTEDElement.setGeometry(geo);
//							// 设置点选中样式
//							SELECTEDElement.setSymbol(srs.Display.Setting.SelectPolygonFeatureStyle);
//							if (fillElements.size()>0&&fillElements !=null) {
//									if (!mBuddyControl.getElementContainer()
//											.getElements().contains(fillElements.get(indexs.indexOf(index)))) {
//										fillElements.add(SELECTEDElement);
//									}else {
//										fillElements.remove(SELECTEDElement);
//									}
//							}else {
//								fillElements.add(SELECTEDElement);
//							}
							if (zoom2Selected != null) {
								zoom2Selected.doEventSettingsChanged(indexs);
							}
//							mBuddyControl.getElementContainer().AddElements(fillElements);
							mBuddyControl.PartialRefresh();
						} else {
							// 设置地图为未选中任何对象
//							if(SELECTEDElement!=null
//									&&mBuddyControl.getElementContainer().getElements().contains(SELECTEDElement)){
//								mBuddyControl.getElementContainer().RemoveElementsE(fillElements);
//							}
							mBuddyControl.PartialRefresh();
						}
						/*应该在else里面写
						 * mBuddyControl.PartialRefresh();*/
					}
					/*修改 20150820  李忠义
					 * 为了让地图能刷新，啥时候都要返回false*/
					return false;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			mExtTime = -1;
			mCurrentPoint = null;
			break;
		}
		return false;
	}
}
